package com.yunupmall.controller.ford;

import com.yunupmall.api.fordVO.CommodityVo;
import com.yunupmall.api.fordentity.Commodity;
import com.yunupmall.api.fordentity.Kind;
import com.yunupmall.api.fordentity.SpecsCommodity;
import com.yunupmall.api.po.IndexKindCommtityPO;
import com.yunupmall.api.po.SpecsCommodityPo;
import com.yunupmall.entity.ford.FordCommodity;
import com.yunupmall.entity.ford.FordCommtoty;
import com.yunupmall.entity.ford.FordSpecsCommodity;
import com.yunupmall.service.admin.service.CommodityService;
import com.yunupmall.service.admin.service.SpecsCommdityService;
import com.yunupmall.service.ford.service.IndexService;
import com.yunupmall.uits.Result;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class indexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private SpecsCommdityService specCommdityService;
    private Integer cId;
    @Autowired
    private  Result result;

    @RequestMapping(value = {"", "/"})
    public String index() {
        return "index";
    }

    @RequestMapping("/head")
    public String head() {
        return "head";
    }

    @GetMapping("/fordquertykind")
    @ResponseBody
    public List<Kind> fordquertykind() {
        List<Kind> fordquertykind = indexService.fordquertykind();
        return fordquertykind;
    }

    /**
     * 主页获取商品信息和类别信息
     * @return
     */
    @PostMapping("/getCommtityData")
    @ResponseBody
    public Result<List<IndexKindCommtityPO>> getCommtityData() {
        try {
            return result.ok(indexService.getKindCommtityData());
        }catch (Exception e){
            return result.error(e.getMessage());
        }
    }

    /**
     * 商品信息详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/skipcommtotyitem")
    public String skipcommtoty(@RequestParam("id") Integer id, Model model) {
        try {
            CommodityVo commodityVo = new CommodityVo();
            commodityVo.setId(id);
            List<Commodity> commodities = commodityService.newcommotityID(commodityVo);
            if (commodities.size() !=0){
                model.addAttribute("commotity", commodities.get(0));
            }
            SpecsCommodityPo specsCommodity = new SpecsCommodityPo();
            specsCommodity.setCId(id);
            List<SpecsCommodity> getskuspecs = specCommdityService.getListData(specsCommodity);
            if (getskuspecs.size() != 0) {
                model.addAttribute("specs", getskuspecs);
            }
            cId=id;
            return "item_show";
        }catch (Exception e) {
            return e.getMessage();
        }

    }

    @GetMapping("/getfordcommtityImage")
    @ResponseBody
    public List<FordCommodity> getfordcommtityImage() {
        List<FordCommodity> getcommtotyimage = indexService.getcommtotyimage(cId);
        return getcommtotyimage;
    }

    //查询商品库存
    @GetMapping("/queryspecsskudata")
    @ResponseBody
    public FordSpecsCommodity queryspecsskudata(Integer id) {
        FordSpecsCommodity queryspecsskudata = indexService.queryspecsskudata(id);
        return queryspecsskudata;
    }
}
